from array import array
from datetime import datetime
from hashlib import sha256
from json import dumps
from typing import List

from flask import Blueprint, jsonify, Flask, current_app, make_response, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restx import Resource, Api, reqparse, inputs

from exts import db
from models import user_role
from models.article import Article
from models.role import Role
from models.user import User
from utils.type import arrayType
from utils.http import fail_api,success_api

role_bp = Blueprint("role",__name__,url_prefix="/role")
@role_bp.get("/")
@jwt_required(optional=False)
def getList():
    per_page = int(request.args.get("per_page") or 10)
    page = int(request.args.get("page") or 1)
    user_name = request.args.get("user_name")
    query = Role.query
    count = query.count()
    data = query.limit(per_page).offset((page - 1) * per_page).all()
    list = {"count":count,"list":data,"per_page":per_page,"page":page}
    return success_api("成功",list)
@role_bp.delete("/<int:id>")
@jwt_required(optional=False)
def deleteRole(id):
    role = Role.query.get(id)
    try:
        db.session.delete(role)
        db.session.commit()
        return success_api("删除成功")
    except:
        return fail_api("删除失败")
@role_bp.patch("/enable/<int:id>")
@jwt_required(optional=False)
def updateEnable(id):
    role = Role.query.get(id)
    try:
        role.enable = 0 if role.enable==True else 1
        #print(role.enable)
        db.session.commit()
        return success_api("更新成功")
    except:
        return fail_api("更新失败")

@role_bp.post("/")
@jwt_required(optional=False)
def createRole():
    parser = reqparse.RequestParser()
    # 2.利用解析器对象添加 需要验证的参数
    parser.add_argument('name', type=str, help='名称验证失败！', required=True, trim=True)
    parser.add_argument('remark', type=str, help='备注验证失败！', trim=True)
    parser.add_argument('details', type=str, help='详情验证失败！', trim=True)
    parser.add_argument('enable', type=str, help='状态验证失败！', trim=True)
    try:
        args = parser.parse_args()
    except Exception as e:
        return {"message": e.data['errors'], "code": 0}
    try:
        role = Role.to_model(**args)
        role.enable = 1 if role.enable==True else 0
        db.session.add(role)
        db.session.commit()
        return success_api("创建成功",role.id)
    except:
        return fail_api("创建失败")
@role_bp.patch("/<int:id>")
@jwt_required(optional=False)
def updateRole(id):
    parser = reqparse.RequestParser()
    # 2.利用解析器对象添加 需要验证的参数
    parser.add_argument('name', type=str, help='名称验证失败！', required=True, trim=True)
    parser.add_argument('remark', type=str, help='备注验证失败！', trim=True)
    parser.add_argument('details', type=str, help='详情验证失败！', trim=True)
    parser.add_argument('enable', type=bool, help='状态验证失败！', trim=True)
    try:
        args = parser.parse_args()
    except Exception as e:
        return {"message": e.data['errors'], "code": 0}
    try:
        role = Role.to_model(**args)
        args.enable = 1 if bool(args.enable) else 0

        Role.query.filter_by(id=id).update(args)
        db.session.commit()
        return success_api("修改成功")
    except:
        return fail_api("修改失败")
@role_bp.get("/<int:id>")
@jwt_required(optional=False)
def getRole(id):
    try:
        role = Role.query.get(id)
        return success_api("获取成功",role)
    except:
        return fail_api("获取失败")